package aa.db;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/User")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String user_id = request.getParameter("user_id");
		String user_account = request.getParameter("user_account");
		String user_pwd = request.getParameter("user_pwd");
		String user_name = request.getParameter("user_name");
		String user_email = request.getParameter("user_email");
		String user_phone = request.getParameter("user_phone");
	    String btn = request.getParameter("btn");
	    
	    String retpage = "user.jsp";
	   
	 //   if("修改".equals(btn)){
	       //update
	  //      sql = "update user_info set user_account='" + user_account +"',user_pwd='" + user_pwd +"',user_name='" + user_name +"',user_email='" + user_email +"',user_phone='" + user_phone +"' where user_id='"+ user_id +"'";
	 //   }else if("del".equals(btn)){
	    	
	    	//delete
	 //       sql = "delete from user_info where user_id='"+ user_id + "'";
	 //   }
	    	
	    if("login".equals(btn)) {
	    	//需要进行身份认证
	    
	     javax.sql.rowset.WebRowSet rsuser = aa.db.DBTools.getRS("SELECT * FROM user_info WHERE user_account='" + user_account+"'");
	     try {
			if(rsuser.next()) {
				 //验证密码
				String dbpwd = rsuser.getString("user_pwd");
			    if(dbpwd.equals(user_pwd)) {
			    	//密码验证通过
			    	
			     	request.getSession().setAttribute("account",user_account);
			     	request.getSession().setAttribute("username",rsuser.getString("user_name"));
			     	
			     	retpage = "index.jsp";
			    }else {
			    	retpage = "user.jsp?ret=3";
			    }
				
			}
			rsuser.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     
	    }else {
	    	  String sql = "";
	  	    //insert
	  	    sql = "insert into user_info(user_account,user_pwd,user_name,user_email,user_phone) values('"+ user_account +"','"+ user_pwd +"','"+ user_name +"','"+ user_email +"','"+ user_phone +"')";
	  	  //执行sql
	  	    
		    int ret = aa.db.DBTools.update(sql);
		    System.out.println("ret=" + ret);
	    	retpage = "user.jsp?ret=" + ret;
	    	
	    }
	    
	   
	    //返回到页面
	    response.sendRedirect(retpage);
	    
	    
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
